package cn.wolfcode.car.web.controller;

import cn.wolfcode.car.domain.Employee;
import cn.wolfcode.car.qo.EmployeeQueryObject;
import cn.wolfcode.car.service.IDepartmentService;
import cn.wolfcode.car.service.IEmployeeService;
import cn.wolfcode.car.service.IRoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequiresPermissions(value = {"employee:list", "员工列表"}, logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        model.addAttribute("pageInfo", employeeService.query(qo));
        model.addAttribute("departments", departmentService.listAll());
        return "employee/list";
    }

    @RequiresPermissions(value = {"employee:delete", "员工删除"}, logical = Logical.OR)
    @RequestMapping("/delete")
    public String delete(Long id){
        if(id != null){
            employeeService.delete(id);
        }
        return "redirect:/employee/list";
    }

    @RequiresPermissions(value = {"employee:input", "员工更新"}, logical = Logical.OR)
    @RequestMapping("/input")
    public String input(Long id, Model model){
        if(id != null){
            model.addAttribute("employee", employeeService.get(id));
        }
        model.addAttribute("departments", departmentService.listAll());
        model.addAttribute("roles", roleService.listAll());
        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee, Long[] roleIds){
        if(employee.getId() != null){
            employeeService.update(employee, roleIds);
        }else{
            employeeService.save(employee, roleIds);
        }
        return "redirect:/employee/list";
    }
}
